package com.woniu.user.util;


import java.util.UUID;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class UUIDExample {
    public static void main(String[] args) {
        // 生成一个UUID
        UUID newUUID = UUID.randomUUID();
        System.out.println("Generated UUID: " + newUUID);

        // 对UUID进行SHA-256加密
        String encryptedUUID = encryptUUID(newUUID.toString());
        System.out.println("Encrypted UUID: " + encryptedUUID);
    }

    public static String encryptUUID(String uuid) {
        try {
            // 获取SHA-256加密实例
            MessageDigest digest = MessageDigest.getInstance("SHA-256");
            // 对UUID进行加密
            byte[] encodedhash = digest.digest(uuid.getBytes());

            // 将加密后的字节转换为十六进制字符串
            StringBuilder hexString = new StringBuilder(2 * encodedhash.length);
            for (byte b : encodedhash) {
                String hex = Integer.toHexString(0xff & b);
                if (hex.length() == 1) {
                    hexString.append('0');
                }
                hexString.append(hex);
            }
            return hexString.toString();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("SHA-256 algorithm not found", e);
        }
    }
}
